package com.ln.drools.controller;


import com.ln.drools.config.ReloadDroolsRules;
import com.ln.drools.model.Address;
import com.ln.drools.model.fact.AddressCheckResult;
import com.ln.drools.utils.KieUtils;
import org.drools.compiler.kproject.models.KieModuleModelImpl;
import org.kie.api.builder.KieRepository;
import org.kie.api.builder.model.KieModuleModel;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.utils.KieHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Collection;


@RequestMapping("/test")
@Controller
public class TestController {

    @Resource
    private ReloadDroolsRules rules;

    @ResponseBody
    @RequestMapping("/address")
    public void test() {
        doFire("ksession-rules");

        doFire("ksession-rules2");

        doFire("stateless-rules");
    }

    private void doFire(String name) {
        System.out.println("doFire(" + name + ")");
        KieSession kieSession = KieUtils.getKieContainer().newKieSession(name);
        Address address = new Address();
        address.setPostcode("994251");

        AddressCheckResult result = new AddressCheckResult();
        kieSession.insert(address);
        kieSession.insert(result);
        int ruleFiredCount = kieSession.fireAllRules();


        System.out.println("触发了" + ruleFiredCount + "条规则");

        if (result.isPostCodeResult()) {
            System.out.println("规则校验通过");
        }

        kieSession.dispose();
    }

    @ResponseBody
    @RequestMapping("/reload")
    public String reload() throws IOException {
        rules.reload();
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/reloadXML")
    public String reloadXML() throws IOException {
        rules.reloadKModuleXML();
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/getConfig")
    public String getConfig() throws IOException {
        KieContainer kc = KieUtils.getKieContainer();
        Collection<String> names = kc.getKieBaseNames();
        System.out.println("KieBaseNames :" + names);
        System.out.println("KieBaseNames :" + names);
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/verify")
    public String verify() throws IOException {


        KieSession kieSession = rules.checkRules();
        int ruleFiredCount = kieSession.fireAllRules();

        System.out.println("触发了" + ruleFiredCount + "条规则");


        kieSession.dispose();
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/addKieModule")
    public String addKieModule() throws IOException {


        KieHelper kh = new KieHelper();
        KieModuleModel kieModule = KieModuleModelImpl.fromXML("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" +
                "    <kbase name=\"rules\" packages=\"rules1\">\n" +
                "        <ksession name=\"ksession-rules\"/>\n" +
                "    </kbase>\n" +
                "</kmodule>");


        KieRepository respo = kh.ks.getRepository();


        return "ok";
    }


}
